<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
if(isset($_REQUEST['submitBtn']))
{
  $partyDetailId  = isset($_POST['partyDetailId']) ? $_POST['partyDetailId'] : 0;
  $partyType      = isset($_POST['partyType']) && ($_POST['partyType'] != "") ? $_POST['partyType'] : 0 ;
  $partyId        = isset($_POST['partyId']) && ($_POST['partyId'] != "") ? $_POST['partyId'] : 0 ;
  $partyDate      = $_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay'];
  $amount         = isset($_POST['amount']) && ($_POST['amount'] != "") ? $_POST['amount'] : 0 ;
  $credit         = isset($_POST['credit']) && ($_POST['credit'] != "") ? $_POST['credit'] : 0 ;
  $transactionIn  = isset($_POST['transactionIn']) && ($_POST['transactionIn'] != "") ? $_POST['transactionIn'] : 0 ;
  $cashPlace      = isset($_POST['cashPlace']) && ($_POST['cashPlace'] != "") ? $_POST['cashPlace'] : 0 ;
  $angadiyaName   = isset($_POST['angadiyaName']) && ($_POST['angadiyaName'] != "") ? $_POST['angadiyaName'] : 0 ;
  $angadiyaCharge = isset($_POST['angadiyaCharge']) && ($_POST['angadiyaCharge'] != "") ? $_POST['angadiyaCharge'] : 0 ;
  $pay            = isset($_POST['pay']) && ($_POST['pay'] != "") ? $_POST['pay'] : 0 ;
  $contactPerson  = isset($_POST['contactPerson']) && ($_POST['contactPerson'] != "") ? $_POST['contactPerson'] : 0 ;
  $angadiya       = isset($_POST['angadiya']) && ($_POST['angadiya'] != "") ? $_POST['angadiya'] : 0 ;
  $angadiyaDate   = $_POST['angadiyaYear']."-".$_POST['angadiyaMonth']."-".$_POST['angadiyaDay'];
  $receiptNo      = isset($_POST['receiptNo']) && ($_POST['receiptNo'] != "") ? $_POST['receiptNo'] : 0 ;
  $type           = isset($_POST['type']) && ($_POST['type'] != "") ? $_POST['type'] : 0 ;
  $narration      = isset($_POST['narration']) && ($_POST['narration'] != "") ? $_POST['narration'] : 0 ;
  	
  $insertPartyDetail = "INSERT INTO partydetail (partyDetailId,partyType,partyId,partyDate,amount,credit,transactionId,
                                                 cashPlace,angadiyaName,angadiyaCharge,pay,contactPerson,angadiya,angadiyaDate,receiptNo,type,narration)
                                          VALUES('".$partyDetailId."',
	                       			                   '".$partyType."',
	                       			                   '".$partyId."',
	                       			                   '".$partyDate."',
	                       			                   '".$amount."',
	                       			                   '".$credit."',
	                       			                   '".$transactionIn."',
	                       			                   '".$cashPlace."',
	                       			                   '".$angadiyaName."',
	                       			                   '".$angadiyaCharge."',
	                       			                   '".$pay."',
	                       			                   '".$contactPerson."',
	                       			                   '".$angadiya."',
	                       			                   '".$angadiyaDate."',
	                       			                   '".$receiptNo."',
	                       			                   '".$type."',
	                       			                   '".$narration."')";
  $inserttPartyDetailRes = mysql_query($insertPartyDetail);
  if(!$inserttPartyDetailRes)
  {
    echo "INSERT FAIL";
  }
  else
  {
    header("Location:partyType.php");
  }
}

// Display All Record of Party
$partyArray = array();
$p = 0;
$selectParty    = "SELECT partyDetailId,partydetail.partyType,partyDate,amount,credit,transactionName,cashPlace,angadiyaName,
                          angadiyaCharge,pay,
                          contactPerson,angadiya,angadiyaDate,receiptNo,type,narration,party.partyName AS partyDetailName
                     FROM partydetail
                     JOIN party ON party.partyId = partydetail.partyId
                     JOIN transaction ON transaction.transactionId = partydetail.transactionId
                 ORDER BY partyDate,partyDetailName";
$selectPartyRes = mysql_query($selectParty);
while($partyRows = mysql_fetch_array($selectPartyRes))
{
	$partyArray[$p]['partyDetailId']   = $partyRows['partyDetailId'];
  $partyArray[$p]['partyType']       = $partyRows['partyType'];
  $partyArray[$p]['partyDetailName'] = $partyRows['partyDetailName'];
  $partyArray[$p]['partyDate']       = $partyRows['partyDate'];
  $partyArray[$p]['amount']          = $partyRows['amount'];
  $partyArray[$p]['credit']          = $partyRows['credit'];
  $partyArray[$p]['transactionIn']   = $partyRows['transactionName'];
  $partyArray[$p]['cashPlace']       = $partyRows['cashPlace'];
  $partyArray[$p]['angadiyaName']    = $partyRows['angadiyaName'];
  $partyArray[$p]['angadiyaCharge']  = $partyRows['angadiyaCharge'];
  $partyArray[$p]['pay']             = $partyRows['pay'];
  $partyArray[$p]['contactPerson']   = $partyRows['contactPerson'];
  $partyArray[$p]['angadiya']        = $partyRows['angadiya'];
  $partyArray[$p]['angadiyaDate']    = $partyRows['angadiyaDate'];
  $partyArray[$p]['receiptNo']       = $partyRows['receiptNo'];
  $partyArray[$p]['type']            = $partyRows['type'];
  $partyArray[$p]['narration']       = $partyRows['narration'];
  $p++;
}
// party combo start
$partyComboArray = array();
$r=0;
$selectPartyCombo = "SELECT partyId,partyName
                       FROM party";
$selectPartyComboRes = mysql_query($selectPartyCombo);
while($partySelectRow = mysql_fetch_array($selectPartyComboRes))
{
	$partyComboArray['partyId'][$r]   = $partySelectRow['partyId'];
	$partyComboArray['partyName'][$r] = $partySelectRow['partyName'];
	$r++;
}
// party combo end

// partyType combo start
$partyTypeComboArray = array();
$r=0;
$selectPartyTypeCombo = "SELECT partyId,partyType
                           FROM party
                       GROUP BY partyType";
$selectPartyTypeComboRes = mysql_query($selectPartyTypeCombo);
while($partyTypeRow = mysql_fetch_array($selectPartyTypeComboRes))
{
	$partyTypeComboArray['partyId'][$r]   = $partyTypeRow['partyId'];
	$partyTypeComboArray['partyType'][$r] = $partyTypeRow['partyType'];
	$r++;
}
// partyType combo end
$tranctionInArray = array();
$i = 0;
$selectTranctionIn = "SELECT transactionName, transactionId
                        FROM transaction";
$selectTranctionInRes = mysql_query($selectTranctionIn);
while($selectTranctionInRow = mysql_fetch_array($selectTranctionInRes))
{
  $tranctionInArray['transactionId'][$i]= $selectTranctionInRow['transactionId'];
  $tranctionInArray['transactionName'][$i] = $selectTranctionInRow['transactionName'];
  $i++;
} 
include("./bottom.php");
$smarty->assign("partyArray",$partyArray);
$smarty->assign("tranctionInArray",$tranctionInArray);
$smarty->assign("partyComboArray",$partyComboArray);
$smarty->assign("partyTypeComboArray",$partyTypeComboArray);
$smarty->display("partyType.tpl");
?>